package net.entelijan.cobean.examples.data.literature.wrapper;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.examples.data.literature.endpoint.WsLitService;
import net.entelijan.cobean.examples.data.literature.endpoint.WsLitService_Service;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

public abstract class WsUtil {

	private WsUtil() {
		super();
	}

	public static ILitService createWrappedService() {
		return new WrappedService(createService(), createMapper());
	}

	private static Mapper createMapper() {
		DozerBeanMapper re = new DozerBeanMapper();
		ArrayList<String> mappingFiles = new ArrayList<String>();
		mappingFiles.add("wrappedLitServiceDozer.xml");
		re.setMappingFiles(mappingFiles);
		return re;
	}

	public static XMLGregorianCalendar createXMLGregorianCalendar(String value,
			String simpleDateFormatPattern)
			throws DatatypeConfigurationException {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatPattern);
			sdf.setLenient(false);
			Date date = sdf.parse(value);
			return createXMLGregorianCalendar(date);
		} catch (ParseException e) {
			throw new DatatypeConfigurationException(e);
		}
	}

	public static XMLGregorianCalendar createXMLGregorianCalendar(Date date)
			throws DatatypeConfigurationException {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		XMLGregorianCalendar re = DatatypeFactory.newInstance()
				.newXMLGregorianCalendar(cal);
		return re;
	}

	public static WsLitService createService() {
		try {
			URL wsdlUrl = createWsdlUrl1();
			return new WsLitService_Service(wsdlUrl, new QName(
					"http://persistence.literature.data.cobean.entelijan.net/",
					"WsLitService")).getWsLitService();
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * Does not work currently because soap:adresse in the wsdl is
	 * localhost:8180 This address is not accessable from the (inter)net
	 */
	@SuppressWarnings("unused")
	private static URL createWsdlUrl() throws MalformedURLException {
		URL wsdlUrl = new URL(
				"http://entelijan.net/cobean-data-ear-cobean-data-0.0.1-SNAPSHOT/PersistenceLitService?wsdl");
		return wsdlUrl;
	}

	private static URL createWsdlUrl1() throws MalformedURLException {
		File file = new File("src/main/wsdl/WsLitService.wsdl");
		if (!file.exists()) {
			throw new IllegalArgumentException("Wsdl '" + file + "' not found");
		}
		return file.toURI().toURL();
	}

	public static void loadProxyProperties() {
		File userHome = new File(System.getProperty("user.home"));
		if (userHome.exists()) {
			File propsFile = new File(userHome, "proxy.properties");
			if (propsFile.exists()) {
				FileReader reader = null;
				try {
					try {
						reader = new FileReader(propsFile);
						System.getProperties().load(reader);
					} finally {
						if (reader != null) {
							reader.close();
						}
					}
				} catch (IOException e) {
					loadProxyPropertiesFromResource();
				}
			} else {
				loadProxyPropertiesFromResource();
			}
		} else {
			loadProxyPropertiesFromResource();
		}
	}

	private static void loadProxyPropertiesFromResource() {
		URL url = WsUtil.class.getClassLoader().getResource("proxy.properties");
		if (url != null) {
			try {
				System.getProperties().load(url.openStream());
			} catch (IOException e) {
				// Nothing to be done
			}
		}

	}

}
